module.exports = class extends think.cmswing.rest {
  /**
   * app供需接口
   * @returns {Promise.<*>}
   */
  async postAction() {
    // 发布人信息
    const faburen = this.post('faburen');
    const xinxileixing = this.post('xinxileixing');
    const xinxibiaoti = this.post('xinxibiaoti');
    const shangpingleibie = this.post('shangpingleibie');
    const leirongmiaoshu = this.post('leirongmiaoshu');
    const diqu = this.post('diqu');
    const suozaiweizhi = this.post('suozaiweizhi');
    const shangpingshuliang = this.post('shangpingshuliang');
    console.log('shangpingshuliang=' + shangpingshuliang);
    const jiage = this.post('jiage');
    const zongjia = this.post('zongjia');
    const chuchenglv = this.post('chuchenglv');
    const zhiliangyaoqiu = this.post('zhiliangyaoqiu');
    const youxiaoqi = this.post('youxiaoqi');
    const tupianid = this.post('tupian');
    let tupian;
    if (tupianid) {
      tupian = await this.model('ext_attachment_pic').where({id: ['IN', tupianid]}).getField('path');
    }
    const mokuaibiaoshi = this.post('mokuaibiaoshi');// 用于区别各个模块的发布
    const fabu_no = await this.model('cmswing/order').orderid(mokuaibiaoshi + faburen);
    const chuangjianshijian = new Date().toLocaleString();
    const create_time = Date.now();
    const expired_time = create_time + get_youxiaotianshu(youxiaoqi) * 86400000;
    const data = {
      fabu_no: fabu_no,
      faburen: faburen,
      xinxileixing: xinxileixing,
      xinxibiaoti: xinxibiaoti,
      shangpingleibie: shangpingleibie,
      leirongmiaoshu: leirongmiaoshu,
      diqu: diqu,
      suozaiweizhi: suozaiweizhi,
      shangpingshuliang: shangpingshuliang,
      jiage: jiage,
      zongjia: zongjia,
      chuchenglv: chuchenglv,
      zhiliangyaoqiu: zhiliangyaoqiu,
      youxiaoqi: youxiaoqi,
      tupian: JSON.stringify(tupian),
      chuangjianshijian: chuangjianshijian,
      create_time: create_time,
      expired_time: expired_time,
      mokuaibiaoshi: mokuaibiaoshi,
      status: 0
    };
    if (!data.hasOwnProperty('mokuaibiaoshi')) {
      data.mokuaibiaoshi = 1;
      data.info_channel = 4;
    } else {
      const map = {
        0: 5,
        1: 4,
        2: 3
      };
      data.info_channel = map[data.mokuaibiaoshi];
    }
    const id = await this.model('gongxufabu').add(data);
    if (!id) return this.fail('信息新增失败');
    /* const xiangqing = {
      挂单编号:fabu_no,
      发布人:faburen,
      信息类型: xinxileixing,
      信息标题: xinxibiaoti,
      商品类别: shangpingleibie,
      内容描述: leirongmiaoshu,
      地区:diqu,
      所在位置: suozaiweizhi,
      商品数量: shangpingshuliang,
      价格: jiage,
      出成率: chuchenglv,
      质量要求:zhiliangyaoqiu,
      有效期:youxiaoqi,
      图片:JSON.stringify(tupian),
      创建时间:chuangjianshijian,
      模块标识:mokuaibiaoshi,
    };
    const dataAll = {
      model:2,
      data:JSON.stringify(xiangqing),
      uid:faburen,
      title:xinxibiaoti,
      time:new Date().getTime(),
    }
    const id2 =await this.model('admin/approval').add(dataAll);
    if (!id2) return this.fail('后台订单新增失败'); */
    return this.success('信息新增成功');
  }

  /**
   * 组合查询
   * @returns {Promise.<*>}
   */
  async getAction() {
    const id = this.get('id');
    const user_id = this.get('user_id');
    const mokuaibiaoshi = this.get('mokuaibiaoshi');
    const diqu = this.get('diqu');
    const shangpingleibie = this.get('shangpingleibie');
    const xinxileixing = this.get('xinxileixing');
    const appmobile = this.get('appmobile');
    const status = this.get('status');
    const groupid = this.get('groupid');
    const map = {};
    map.status = status || 5;
    if (diqu) { map.diqu = ['LIKE', '%' + diqu + '%'] }
    if (shangpingleibie) { map.shangpingleibie = ['LIKE', '%' + shangpingleibie + '%'] }
    if (xinxileixing) { map.xinxileixing = xinxileixing }
    let orderAll;
    // 单个详情
    if (id) {
      orderAll = await this.model('cmswing/gongxufabu').where({id: id}).order('id DESC').select();
      return this.success(orderAll, '订单查询成功');
    }
    // 个人发布
    if (user_id) {
      const status = this.get('status') || null;
      const statusMap = {
        null: {faburen: user_id},
        0: {
          status: 0,
          faburen: user_id
        },
        5: {
          status: 5,
          faburen: user_id
        }
      };
      const map = statusMap[status];
      orderAll = await this.model('cmswing/gongxufabu').where(map).order('id DESC').page(this.para('page') || 1, 10).select();
      return this.success(orderAll, '订单查询成功');
    }
    // 我的信息
    if (appmobile) {
      const status = this.get('status') || null;
      const statusMap = {
        null: {appmobile: appmobile, status: ['IN', [3, 4]]},
        3: {
          status: 3,
          appmobile: appmobile
        },
        4: {
          status: 4,
          appmobile: appmobile
        }
      };
      const map = statusMap[status];
      orderAll = await this.model('cmswing/gongxufabu').where(map).order('id DESC').page(this.para('page') || 1, 10).select();
      return this.success(orderAll, '订单查询成功');
    }
    // 更多供需模块
    if (mokuaibiaoshi == 3) {
      if (mokuaibiaoshi) { map.mokuaibiaoshi = ['IN', [1, 2]] }
      const data = await this.model('member').where({groupid: [(groupid == 12 ? '!=' : '='), 12]}).getField('id');
      map.faburen = ['IN', data];
      if (shangpingleibie) {
        orderAll = await this.model('gongxufabu').where(map).order('id DESC').page(this.para('page') || 1, 10).select();
        return this.success(orderAll, '订单查询成功');
      } else {
        orderAll = await this.model('gongxufabu').where(map).order('id DESC').page(this.para('page') || 1, 10).select();
        return this.success(orderAll, '订单查询成功');
      }
    }
    if (mokuaibiaoshi == 0 || mokuaibiaoshi == 1 || mokuaibiaoshi == 2) {
      if (mokuaibiaoshi) { map.mokuaibiaoshi = mokuaibiaoshi }
      orderAll = await this.model('gongxufabu').where(map).order('id DESC').page(this.para('page') || 1, 10).select();
      return this.success(orderAll, '订单查询成功');
    }
    return this.fail('订单查询失败');
  }
};
